package com.estar.app.business.mobileLogin.controller;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.estar.app.appsrvyyabx.mobile.domain.DataVO;
import com.estar.app.business.mobileLogin.utils.CheckDangerTimesUtil;
import com.estar.app.business.mobileLogin.utils.DutyUtil;
import com.estar.app.business.mobileLogin.utils.JinYouUtil;
import com.estar.app.business.mobileLogin.utils.RespCheckUtil;
import com.thoughtworks.xstream.XStream;

@Controller("mobileServlet")
@Scope("prototype")
public class MobileServlet {

	@Autowired
	RespCheckUtil respCheckUtil;
	@Autowired
	DutyUtil dutyUtil;
	@Autowired
	JinYouUtil jinYouUtil;
	@Autowired
	CheckDangerTimesUtil checkDangerTimesUtil;
	private static final Logger logger = Logger.getLogger(MobileServlet.class);
	@RequestMapping(params = "mobile")
	public void mobile(HttpServletRequest request,HttpServletResponse response){
		DataVO dataReturnVO = new DataVO();
		DataVO dataSendVO = new DataVO();
		InputStreamReader isr = null;
		BufferedReader in =null;
		
		
		try {
			isr = new InputStreamReader(request.getInputStream(), "UTF-8");
			in = new BufferedReader(isr);
			StringBuffer inStr = new StringBuffer("");
			String inputLine;
			while ((inputLine = in.readLine()) != null) {
				inStr.append(inputLine);
				inStr.append("\n");
			}
			in.close();
			if (inStr == null || "".equals(new String(inStr))) {
				dataReturnVO.setResultCde("0");
				dataReturnVO.setMessage("接收的数据为空");
			} else {
					
				String sendXml = URLDecoder.decode(inStr.toString(), "UTF-8");	// 转码
				
				dataSendVO = (DataVO) new XStream().fromXML(sendXml);
				
				//是否出险
				if(dataSendVO.getAction().trim().equals("dangerOrNot")){
					//查询出险次数
					if (dataSendVO.getMethod().trim().equals("selectDangerTimes")){			
						dataReturnVO = checkDangerTimesUtil.getDangerTimes(dataSendVO);
					}
				}
				
				// 用户相关
				if (dataSendVO.getAction().trim().equals("user")){		
					
					// 登录验证
					if (dataSendVO.getMethod().trim().equals("login")){			
						dataReturnVO = respCheckUtil.login(dataSendVO);
					}
					
					//密码修改
					if(dataSendVO.getMethod().trim().equals("changePasswork")){
						dataReturnVO= respCheckUtil.changePasswork(dataSendVO);
					}
				}
				
				//案件相关
				if(dataSendVO.getAction().trim().equals("task")){
					
					//案件创建
					if (dataSendVO.getMethod().trim().equals("create")){	
						System.out.println("案件创建："+sendXml);
						dataReturnVO = respCheckUtil.taskCreate(dataSendVO);
					}
					
					//案件暂存
					if (dataSendVO.getMethod().trim().equals("temporary")){		
						System.out.println("案件暂存："+sendXml);
						dataReturnVO = respCheckUtil.taskTemp(dataSendVO);
					}
					
					//案件提交
					if (dataSendVO.getMethod().trim().equals("submit")){	
						System.out.println("案件提交："+sendXml);
						dataReturnVO = respCheckUtil.taskSubmit(dataSendVO);
					}
					
					//案件注销
					if (dataSendVO.getMethod().trim().equals("destroy")){
						System.out.println("案件注销："+sendXml);
						dataReturnVO = respCheckUtil.taskDestroy(dataSendVO);
					}
					
					//案件查询
					if(dataSendVO.getMethod().trim().equals("caseSearch")){
						dataReturnVO = respCheckUtil.taskSearch(dataSendVO);
					}
					
					//短信发送
					if(dataSendVO.getMethod().trim().equals("sendIdentifyingCode")){
						dataReturnVO = respCheckUtil.sendIdentifyingCode(dataSendVO);
					}
					
					//短信验证
					if(dataSendVO.getMethod().trim().equals("checkIdentifyingCode")){
						dataReturnVO = respCheckUtil.checkIdentifyingCode(dataSendVO);
					}
					
					//案件修改
					if(dataSendVO.getMethod().trim().equals("taskUpdate")){
						dataReturnVO = respCheckUtil.taskUpdate(dataSendVO);
					}
					
					//案件修改
					if(dataSendVO.getMethod().trim().equals("getVersion")){
						dataReturnVO = respCheckUtil.getVersion(dataSendVO);
					}
					/*案件审核*/
					if(dataSendVO.getMethod().trim().equals("surveyExamine")){
						dataReturnVO = respCheckUtil.surveyExamine(dataSendVO);
					}
					
				}
				
				//图片相关
				if(dataSendVO.getAction().trim().equals("qAndroidUploadImg")){
					
					//图片上传
					if (dataSendVO.getMethod().trim().equals("uploadImg")){			
						dataReturnVO = respCheckUtil.uploadImg(dataSendVO);
					}
					//从后台获取图片
					if(dataSendVO.getMethod().trim().equals("getPic")){
						dataReturnVO = respCheckUtil.getPic(dataSendVO,request);
					}
				}
				
				//上下班记录功能
				if(dataSendVO.getAction().trim().equals("workTime")){
					
					//上班
					if (dataSendVO.getMethod().trim().equals("onDuty")){			
						dataReturnVO = dutyUtil.onDuty(dataSendVO);
					}
					//下班
					if(dataSendVO.getMethod().trim().equals("offDuty")){
						dataReturnVO = dutyUtil.offDuty(dataSendVO);
					}
				}
				
				//精友移动定损
				if(dataSendVO.getAction().trim().equals("jinYou")){
					
					//获取信息
					if (dataSendVO.getMethod().trim().equals("getMess")){			
						dataReturnVO = jinYouUtil.getMess(dataSendVO);
					}
				}
			}
		} catch (Exception e) {
			dataReturnVO.setResultCde("0");
			dataReturnVO.setMessage("系统错误。");
			logger.error("系统异常：",e);
		} finally {		// 返回数据
			String outStr = new XStream().toXML(dataReturnVO);
			
			try {
				if(in!=null){
					in.close();
				}
				if(isr!=null){
					isr.close();
				}
				
				response.setContentType("text/html; charset=UTF-8");
				PrintWriter write = response.getWriter();
				write.write(outStr);
				write.close();
			} catch (Exception ex) {
				logger.error("系统异常：",ex);
			}
		}
	}
}
